import { Component, OnInit } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { FormBuilder, Validators } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';

import { ILogistic, Logistic } from 'app/shared/model/logistic.model';
import { LogisticService } from './logistic.service';

@Component({
  selector: 'jhi-logistic-update',
  templateUrl: './logistic-update.component.html',
})
export class LogisticUpdateComponent implements OnInit {
  isSaving = false;

  editForm = this.fb.group({
    id: [],
    orderId: [],
    sendName: [],
    sendPhone: [],
    sendAddress: [],
    receiveName: [],
    receivePhone: [],
    receiveAddress: [],
    logisticNo: [],
    logisticCompany: [],
    sendTime: [],
    receivedTime: [],
  });

  constructor(protected logisticService: LogisticService, protected activatedRoute: ActivatedRoute, private fb: FormBuilder) {}

  ngOnInit(): void {
    this.activatedRoute.data.subscribe(({ logistic }) => {
      this.updateForm(logistic);
    });
  }

  updateForm(logistic: ILogistic): void {
    this.editForm.patchValue({
      id: logistic.id,
      orderId: logistic.orderId,
      sendName: logistic.sendName,
      sendPhone: logistic.sendPhone,
      sendAddress: logistic.sendAddress,
      receiveName: logistic.receiveName,
      receivePhone: logistic.receivePhone,
      receiveAddress: logistic.receiveAddress,
      logisticNo: logistic.logisticNo,
      logisticCompany: logistic.logisticCompany,
      sendTime: logistic.sendTime,
      receivedTime: logistic.receivedTime,
    });
  }

  previousState(): void {
    window.history.back();
  }

  save(): void {
    this.isSaving = true;
    const logistic = this.createFromForm();
    if (logistic.id !== undefined) {
      this.subscribeToSaveResponse(this.logisticService.update(logistic));
    } else {
      this.subscribeToSaveResponse(this.logisticService.create(logistic));
    }
  }

  private createFromForm(): ILogistic {
    return {
      ...new Logistic(),
      id: this.editForm.get(['id'])!.value,
      orderId: this.editForm.get(['orderId'])!.value,
      sendName: this.editForm.get(['sendName'])!.value,
      sendPhone: this.editForm.get(['sendPhone'])!.value,
      sendAddress: this.editForm.get(['sendAddress'])!.value,
      receiveName: this.editForm.get(['receiveName'])!.value,
      receivePhone: this.editForm.get(['receivePhone'])!.value,
      receiveAddress: this.editForm.get(['receiveAddress'])!.value,
      logisticNo: this.editForm.get(['logisticNo'])!.value,
      logisticCompany: this.editForm.get(['logisticCompany'])!.value,
      sendTime: this.editForm.get(['sendTime'])!.value,
      receivedTime: this.editForm.get(['receivedTime'])!.value,
    };
  }

  protected subscribeToSaveResponse(result: Observable<HttpResponse<ILogistic>>): void {
    result.subscribe(
      () => this.onSaveSuccess(),
      () => this.onSaveError()
    );
  }

  protected onSaveSuccess(): void {
    this.isSaving = false;
    this.previousState();
  }

  protected onSaveError(): void {
    this.isSaving = false;
  }
}
